﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WcfMail.Interface;
using PWMIS.DataMap.Entity;

namespace WcfMail.Common
{
    public class PdfNetConvert : IConvert 
    {
        //ISO8859-1 字符串，8位，只有这种可以完整保留二进制
        Encoding _encoding = Encoding.GetEncoding(28591);

        #region IConvert 成员

        public T ConvertToObject<T>(string objectString)
        {
            throw new NotImplementedException();
        }

        public object ConvertToObject(string objectString, Type targetType)
        {
            //byte[] buffer = Convert.FromBase64String(objectString);
            byte[] buffer = _encoding.GetBytes(objectString);
            //System.Diagnostics.Debug.WriteLine("ConvertToObject buffer Length:" + buffer.Length .ToString ());
            return PdfNetSerialize.BinaryDeserialize(buffer, targetType);
        }

        public string ConvertToString(object targetObject)
        {
            if (targetObject is EntityBase)
            {
                byte[] buffer= PdfNetSerialize.BinarySerialize((EntityBase)targetObject);
                //return Convert .ToBase64String (buffer );
                //System.Diagnostics.Debug.WriteLine("ConvertToString buffer Length:" + buffer.Length .ToString());
                return _encoding.GetString(buffer);
            }
            else
            {
                throw new Exception("PdfNetConvert 失败：当前参数不是有效的PDF.NET实体类。");
            }
        }

        #endregion
    }
}
